<?php

namespace app\controller;

use app\BaseController;
use app\model\Users;
use app\services\AuthService;
use app\services\UserService;
use app\validate\UserValidate;

use think\App;

class UserController extends BaseController
{


    /**
     * @var UserService
     */
    private $userService;
    /**
     * @var AuthService
     */
    private $authService;

    public function __construct(App $app)
    {
        $this->userService = new UserService();
        $this->authService = new AuthService();
        parent::__construct($app);
    }

    public function addUser()
    {

        $param = $this->request->param(['username', 'password']);
        $param['role'] = $this->request->param('role', 1);
        validate(UserValidate::class)
            ->scene('add')
            ->check($param);


        return $this->returnJson($this->userService->add($param['role'], $param['username'], $param['password']));
    }

    public function getUser()
    {
        $keyword = $this->request->param('keyword', '');
        $usersDao = new Users();
        return $this->returnJson($usersDao->where('username', 'like', '%'.$keyword.'%')->select());
    }

    public function getList()
    {
        $keyword = $this->request->param('keyword', '');
        $usersDao = new Users();
        return $this->returnJson($usersDao->where('username', 'like', '%'.$keyword.'%')->field('id,username')->select());
    }
    public function getUserById(Users $users )
    {
        return $this->returnJson($users);
    }


    public function getUserInfo()
    {
        return $this->returnJson();
    }

    public function login()
    {
        $param = $this->request->param(['password', 'username']);
        validate(UserValidate::class)
            ->scene('login')
            ->check($param);

        return $this->returnJson($this->authService->login($param['username'], $param['password']));

    }


}